package com.shopping_assistant.view.tabs;

import com.shopping_assistant.model.Offer;
import com.shopping_assistant.model.Person;
import com.shopping_assistant.model.PersonController;
import com.shopping_assistant.model.ShopController;
import com.shopping_assistant.view.OfferView;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class TopTenOffersTab extends ListActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void onResume() {
		super.onResume();

		ArrayAdapter<Offer> arrayAdapter = new ArrayAdapter<Offer>(this,
				android.R.layout.simple_list_item_1, ShopController
						.getInstance().getAllOffers());

		setListAdapter(arrayAdapter);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Offer selectedItem = (Offer) l.getItemAtPosition(position);

		selectedItem.countUpTotalViews();

		PersonController.getInstance().getActiveUser()
				.offerViewed(selectedItem);

		Intent offerDetailsView = new Intent(TopTenOffersTab.this,
				OfferView.class);
		offerDetailsView.putExtra("offerid", selectedItem.getOfferId());
		startActivity(offerDetailsView);

		super.onListItemClick(l, v, position, id);
	}
}
